<%@ page contentType="text/html;charset=UTF-8" language="java"%>
<html>
<head>
<title>Title</title>
<script type="text/javascript">
	var stationManager = function() {

		var stationListTable;
		var selectMenuObj;
		var roleObj = {};
		var isEdit = 0; //0是新增，1是修改

		var getRawData = function() {
			var rows = $("#stationList tr");
			$.each(rows, function(index, row) {
				if (index > 0) {
					$(row).click(function() {
						$(row).addClass("info");
						$(row).siblings().removeClass("info");
						
					});
				}
			});
		}
		var initStationList = function() {

			stationListTable = $('#stationList').dataTable({
				fnDrawCallback : getRawData,
				"oLanguage" : {
					"sInfoEmpty" : "",
					"sInfo" : "当前第 _START_ - _END_ 个&nbsp;总共 _TOTAL_ 个",
					"sEmptyTable" : "没有找到符合条件的数据",
					"sLoadingRecords" : "加载中...",
					"sSearch" : "<i class=\"fa fa-search\"></i>&nbsp;搜索",
					"oPaginate" : {
						"sFirst" : "首页",
						"sLast" : "尾页",
						"sNext" : "下一页",
						"sPrevious" : "上一页"
					}
				},
				"sPaginationType" : "bootstrap",
				"sDom" : '<<"pull-left tableSearch"f>>rt<<"col-sm-6"i><"col-sm-6"p>>',
				"iDisplayLength" : 10, // default record count per page
				"sAjaxSource" : ctx + '/sys/station/list.do',
				//searchText: $('#searchText').val()
				//"fnServerParams":[{"name":"searchText","value":$('#searchInput').val()}],
				 "fnServerData" : function(sSource, aDataSet, fnCallback) {  
					 var searchText = $('#searchInput').val()==null?"":$('#searchInput').val();
					
					 aDataSet.push({"name":"searchText","value":searchText});
					 
                                $.ajax({  
                                    "dataType" : 'json',  
                                    "type" : "GET",  
                                    "url" : sSource,  
                                    "data" : aDataSet,  
                                    "success" : fnCallback  
                                });  
                            }  ,  
				"sServerMethod" : "get",
				"serverSide" : true,
				"bAutoWidth" : false,
				"bServerSide" : true,
				"bFilter" : false,// 搜索栏
				"bSort" : false,// 排序
				"aoColumns" : [ {
					"mData" : "ROLE_CODE",
					"sTitle" : "岗位代码",
					"visible" : false,
					"sWidth" : "0px"
				}, {
					"mData" : "ROLE_TYPE",
					"sTitle" : "岗位类型",
					"visible" : false,
					"sWidth" : "0px"
				}, {
					"mData" : "ROLE_NAME",
					"sTitle" : "岗位名称",
					"sWidth" : "200px"
				}, {
					"mData" : "ROLE_DESC",
					"sTitle" : "岗位描述",
					"sWidth" : "200px"
				}, {
					"mData" : "REAL_NAME",
					"sTitle" : "创建人",
					"sWidth" : "200px"
				} ]
			});
			$('.tableSearch').append("<input type='text'>");
			$('.tableSearch').append("<button class=\"mb-sm btn btn-info\" id=\"searchBtn\"><em class=\"fa fa-search\">&nbsp;搜索</em></label>")
			$('.tableSearch input').css('width', '250px');
			$('.tableSearch input').css('border', '1');
			$('.tableSearch input').css('margin-right', '8px');
			$('.tableSearch input').css('margin-bottom', '5px');
			$('.tableSearch input').attr('id', 'searchInput');
			$('.tableSearch input').attr('placeholder', '模糊查询');
			

		}
		var initMenuTree = function(treeId) {
			var menuTreeSetting = {
				check : {
					enable : true
				},
				data : {
					simpleData : {
						enable : true,
						idKey : "menuCode",
						pIdKey : "parentCode",
						rootPId : '0'
					}
				}
			};
			$.ajax({
				type : 'get',
				dataType : 'json',
				url : ctx + "/sys/station/sysmenu.do",
				contentType : 'charset=utf-8',
				data : {
					"ROLE_CODE" : $("#ROLE_CODE").val()
				},
				success : function(msg) {
					var data = msg.menus;
					$(data).each(function(i, n) {
						if (n.menuType == 0) {
							n.icon = ctx + "/assets/img/btn/user.png";
						} else {
							n.icon = ctx + "/assets/img/btn/house.png";
						}
						n.name = n.menuName;
						
						
						if(n.checked!='unchecked'){
							n.checked = true;
						}else{
							n.checked = false;
						}
						

					});
					var menuTree = $.fn.zTree.init($('#' + treeId),
							menuTreeSetting, data);
					menuTree.expandAll(true);
				}
			});
		}

		var setRoleValue = function() {
			roleObj.roleCode = $("#ROLE_CODE").val();
			roleObj.roleType = $("#ROLE_TYPE").val();
			roleObj.parentCode = $("#PARENT_CODE").val();
			roleObj.roleName = $("#ROLE_NAME").val();
			roleObj.roleDesc = $("#ROLE_DESC").val();
			roleObj.menuList = [];

			for (var i = 0; i < selectMenuObj.length; i++) {
				var node = selectMenuObj[i];
				roleObj.menuList.push(node.menuCode);
			}
		}

		return {
			init : function() {
				initStationList();
				$("#searchBtn").click(function () {
					stationListTable.api().ajax.reload();
				});
			},
			initMenuTree : function(treeId) {
				initMenuTree(treeId);
			},
			saveMenu : function() {
				var treeObj = $.fn.zTree.getZTreeObj("menuList");
				selectMenuObj = treeObj.getCheckedNodes(true);
				return selectMenuObj;
			},
			validatAddStationForm : function() {
				var roleName = $("#ROLE_NAME").val();
				var roleMenuName = $("#ROLE_MENU_NAME").val();

				if (roleName == '') {
					notyMsg('请填写岗位名称！');
					return false;
				}
				if (roleMenuName == '') {
					notyMsg('请给岗位设置权限！');
					return false;
				}

				setRoleValue();

				return true;

			},
			getRoleObj : function() {
				return roleObj;
			},
			reloadStationListTable:function(){
				stationListTable.api().ajax.reload();
			},
			getSelectStationObj:function(){
				var data = stationListTable.api().rows('.info').data();
				if(data.length>0){
					return data[0];
				}else{
					return null;
				}
				
			},
			editWinShownEvent:function(){
				if(isEdit==0){ //新增
					
				}else{ //修改
					var data = stationListTable.api().rows('.info').data()[0];
					$("#ROLE_NAME").val(data.ROLE_NAME);
					 $("#ROLE_DESC").val(data.ROLE_DESC);
					 $("#ROLE_CODE").val(data.ROLE_CODE);
					 var str = "";
					 $.ajax({
							type : 'get',
							dataType : 'json',
							url : ctx + "/sys/station/getRoleMenuName.do",
							contentType : 'charset=utf-8',
							data : {
								"ROLE_CODE" : $("#ROLE_CODE").val(),
							},
							success : function(msg) {
								var data = msg.menus;
								$(data).each(function(i, n) {
									if(i+1<data.length){
										str += n.MENU_NAME+','
									}else{
										str += n.MENU_NAME
									}
									
								});
								$("#ROLE_MENU_NAME").val(str);
								
							}
						});
					 
					 
					 
					 
				}
			},
			setIsEdit:function(value){
				isEdit = value;
			},
			hideEditWin:function(){
				$("#ROLE_NAME").val("");
				 $("#ROLE_DESC").val("");
				 $("#ROLE_CODE").val("");
				 $("#ROLE_MENU_NAME").val("");
				 isEdit = 0;
			},
			removeStation:function($noty){
				var data = stationListTable.api().rows('.info').data()[0];
				$.ajax({
					type : 'get',
					dataType : 'json',
					url : ctx + "/sys/station/delete.do",
					contentType : 'charset=utf-8',
					data : {
						"ROLE_CODE" : data.ROLE_CODE,
					},
					success : function(msg) {
						notyMsg('删除成功!');
						
						stationListTable.api().ajax.reload();
						$noty.close();
					}
				});
			}
			
		}
	}();

	$(function() {
		stationManager.init();
		$('.chosen-select').chosen();
		$('#selectMenu').on('shown.bs.modal', function(e) {
			stationManager.initMenuTree('menuList');
		});
		$('#addWin').on('shown.bs.modal', function(e) {
			stationManager.editWinShownEvent();
		});
		$('#addWin').on('hidden.bs.modal', function(e) {
			stationManager.hideEditWin();
		});
		
		
	});

	//显示选择菜单
	function showSelectMenuList() {
		show_sys_modal('selectMenu');
	}

	function showAddWin() {
		stationManager.setIsEdit(0);
		show_sys_modal('addWin');
	}
	
	function showEditWin(){
		if(stationManager.getSelectStationObj()!=null){
			stationManager.setIsEdit(1);
			show_sys_modal('addWin');
		}else{
			notyMsg('请选择要编辑的数据!');
		}
		
	}

	function saveMenu() {
		var rs = stationManager.saveMenu();
		var str = "";
		for (var i = 0; i < rs.length; i++) {
			var node = rs[i];
			if (i == rs.length - 1) {
				str += node.menuName;
			} else {
				str += node.menuName + ",";
			}
		}
		$('#ROLE_MENU_NAME').val(str);
		$('#selectMenu').modal('hide')
	}

	function saveStation() {
		if (stationManager.validatAddStationForm()) {
			var roleObj = stationManager.getRoleObj();
			$.ajax({
				type : "post",//使用get方法访问后台
				dataType : "json",//返回json格式的数据
				url : ctx + "/sys/station/save.do",//要访问的后台地址
				data : {
					"ROLE_CODE" : roleObj.roleCode,
					"ROLE_TYPE" : roleObj.roleType,
					"PARENT_CODE" : roleObj.parentCode,
					"ROLE_NAME" : roleObj.roleName,
					"ROLE_DESC" : roleObj.roleDesc,
					"menuID" : roleObj.menuList
				},
				success : function(msg) {//msg为返回的数据，在这里做数据绑定
					notyMsg('保存成功!');
					$('#addWin').modal('hide');
					stationManager.reloadStationListTable();
				}
			});
		}

	}
	
	function removeRecord(){
		if(stationManager.getSelectStationObj()!=null){
			notyConfirm("是否确定删除？",function($noty){
				stationManager.removeStation($noty);
			});
		}else{
			notyMsg('请选择要删除的数据!');
		}
		
	}
	
	
	
</script>
</head>
<body>
	<div class="content-wrapper">
		<h3>系统管理/岗位维护</h3>
		<div class="row">
			<div class="col-lg-12">
				<div class="panel panel-info">
					<div class="panel-heading">
						<div class="text-left">
							<em class="fa fa-users fa-2x">&nbsp;岗位列表</em>
							<div class="pull-right">
								<button type="button" class="mb-sm btn btn-success"
									onclick="showAddWin()">
									<em class="fa fa-pencil">&nbsp;添加</em>
								</button>
								<button type="button" class="mb-sm btn btn-warning"
								 onclick="showEditWin()">
									<em class="fa fa-edit">&nbsp;编辑</em>
								</button>
								<button type="button" class="mb-sm btn btn-danger"  onclick="removeRecord()">
									<em class="fa fa-remove">&nbsp;删除</em>
								</button>
							</div>

						</div>
					</div>
					<div class="panel-body">
						<table class="table table-bordered table-hover" id="stationList">
						</table>
					</div>
				</div>
			</div>

		</div>

	</div>
	<div class="modal fade" id="addWin" data-backdrop="static">
		<div class="modal-dialog">
			<div class="modal-content">
				<div class="modal-header">
					<button type="button" class="close" data-dismiss="modal"
						aria-label="Close">
						<span aria-hidden="true">&times;</span>
					</button>
					<h4 class="modal-title">岗位维护</h4>
				</div>
				<div class="modal-body">
					<form method="get" class="form-horizontal" method="post"
						id="addStation" style="height: 180px;">
						<div class="form-group">
							<label class="col-sm-2 control-label">岗位名称:</label>
							<div class="col-sm-10">
								<input type="hidden" id="PARENT_CODE" name="PARENT_CODE"
									value="ALL_MENU_ROLE"> <input type="hidden"
									id="ROLE_TYPE" name="ROLE_TYPE" value="0"> <input
									type="hidden" id="ROLE_CODE" name="ROLE_CODE"> <input
									id="ROLE_NAME" name="ROLE_NAME" required type="text"
									placeholder="" class="form-control">
							</div>
						</div>
						<div class="form-group">
							<label class="col-sm-2 control-label">岗位描述:</label>
							<div class="col-sm-10">
								<input id="ROLE_DESC" name="ROLE_DESC" type="text"
									placeholder="" class="form-control">
							</div>
						</div>
						<div class="form-group">
							<label class="col-sm-2 control-label">岗位权限:</label>
							<div class="col-sm-10">
								<div class="input-group m-b">
									<input id="ROLE_MENU_NAME" name="ROLE_MENU_NAME"
										readonly="readonly" type="text" class="form-control">
									<input type="hidden" id="ROLE_MENU" name="ROLE_MENU"> <span
										class="input-group-btn">
										<button type="button" onclick="showSelectMenuList()"
											class="btn ">选择</button>
									</span>
								</div>
							</div>
						</div>
					</form>
				</div>
				<div class="modal-footer">
					<button type="button" data-dismiss="modal" class="btn btn-default">关闭</button>
					<button type="button" class="btn btn-primary"
						onclick="saveStation()">保存</button>
				</div>
			</div>
			<!-- /.modal-content -->
		</div>
		<!-- /.modal-dialog -->
	</div>

	<div class="modal fade" id="selectMenu" data-backdrop="static">
		<div class="modal-dialog">
			<div class="modal-content">
				<div class="modal-header">
					<button type="button" class="close" data-dismiss="modal"
						aria-label="Close">
						<span aria-hidden="true">&times;</span>
					</button>
					<h4 class="modal-title">功能菜单</h4>
				</div>
				<div class="modal-body">
					<div>
						<ul id="menuList" class="ztree"
							style="height: 350px; overflow-y: auto"></ul>
					</div>
				</div>
				<div class="modal-footer">
					<button type="button" class="btn btn-default" onclick="saveMenu()">关闭</button>
					<button type="button" class="btn btn-primary" onclick="saveMenu()">保存</button>
				</div>
			</div>
			<!-- /.modal-content -->
		</div>
		<!-- /.modal-dialog -->
	</div>





</body>

</html>